package com.javalong.affairs.base.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Base64;

public class AppDataUtils {

    private static final String SHARED_PREFERENCE = "XUNBAOZL";
    private static final String DEFAULT_STRING_VALUE = "NOVALUE";
    private static final String SELECTED_ALUMB = "SELECTED_ALUMB";

    // 图片裁剪比例是否为最佳（1：1）
    public static final String IS_BEST_CROP = "IS_BEST_CROP";
    public static final String IS_FIRST_TIME_USE = "isFirstTimeUse";
    private static final String Reply_Tip = "点击评论可以直接回复哦";
    private static final String Remind_Tip = "@你的好友一起评论吧";
    private static final String Tag_Tip = "输入#讨论你感兴趣的标签吧";
    private static final String COMMENT_TIP = "COMMENT_TIP";
    // configVo
    public static final String CONFIG_VO = "config";
    private static Context mContext;
    private static List<String> tipList;

    static {
        tipList = new ArrayList<String>();
        tipList.add(Reply_Tip);
        tipList.add(Remind_Tip);
        tipList.add(Tag_Tip);
    }

    // 保存对象
    public static void saveObject(Context context, String key, Object object) {
        SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCE,
                Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        if (object == null) {
            editor.remove(key);
        } else {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            try {
                ObjectOutputStream oos = new ObjectOutputStream(bos);
                oos.writeObject(object);
                editor.putString(
                        key,
                        new String(Base64.encode(bos.toByteArray(),
                                Base64.DEFAULT)));
                editor.commit();
                bos.close();
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 保存对象
    public static void saveObject(String key, Object object) {
        saveObject(mContext, key, object);
    }

    public static Object getObject(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCE,
                Context.MODE_PRIVATE);

        String value = sp.getString(key, DEFAULT_STRING_VALUE);
        if (DEFAULT_STRING_VALUE.equals(value)) {
            return null;
        } else {
            ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decode(
                    value.getBytes(), Base64.DEFAULT));
            try {
                ObjectInputStream ois = new ObjectInputStream(bis);
                Object object = ois.readObject();
                ois.close();
                bis.close();
                return object;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static void saveString(Context context, String key, String value) {
        SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCE,
                Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public static void saveBoolean(Context context, String key, boolean value) {
        SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCE,
                Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public static boolean getBoolean(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCE,
                Context.MODE_PRIVATE);
        return sp.getBoolean(key, false);
    }

    public static String getString(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCE,
                Context.MODE_PRIVATE);
        String value = sp.getString(key, DEFAULT_STRING_VALUE);
        if (DEFAULT_STRING_VALUE.equals(value)) {
            return null;
        }
        return value;
    }

    public static void remove(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCE,
                Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.remove(key);
        editor.commit();
    }

}
